package com.ms.p2p.plugin.storage;


import com.ms.common.bean.FileInfo;
import com.ms.common.exception.ApplicationException;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component(value="filePlugin")
public class FilePlugin extends StoragePlugin implements ServletContextAware {

	private ServletContext servletContext;
	
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public void upload(String path, File file, String contentType){
		File file1 = new File(getPath(path));
		try {
			FileUtils.moveFile(file, file1);
		} catch (IOException ioexception) {
			ioexception.printStackTrace();
			throw new ApplicationException("上传文件失败!");
		}
	}

	@Override
	public String getUrl(String path) {
		JSONObject configs = getConfig();
		if (configs != null) {
			String url = configs.getString("url");
			return new StringBuilder(url).append(path).toString();
		} 
		return null;
	}

	@Override
	public List<FileInfo> browser(String path) {
		List<FileInfo> arraylist = new ArrayList<FileInfo>();
		File file = new File(getPath(path));
		if (file.exists() && file.isDirectory()) {
			File[] afile = file.listFiles();
			int j = afile.length;
			for (int i = 0; i < j; i++) {
				File file1 = afile[i];
				FileInfo fileinfo = new FileInfo();
				fileinfo.setName(file1.getName());
				fileinfo.setStorePath(this.getId() + "://" + path + file1.getName());
				fileinfo.setUrl(getUrl(path)+file1.getName());
				fileinfo.setIsDirectory(file1.isDirectory());
				fileinfo.setSize(file1.length());
				fileinfo.setLastModified(new Date(file1.lastModified()));
				arraylist.add(fileinfo);
			}

		}
		return arraylist;
	}

	@Override
	public void moveLocal(String path, File file) {
		File file1 = new File(servletContext.getRealPath(path));
		try {
			FileUtils.copyFile(file, file1);
			FileUtils.deleteQuietly(file);
		} catch (IOException ioexception) {
			ioexception.printStackTrace();
			throw new ApplicationException("上传文件失败!");
		}
	}
	
	@Override
	public void copyLocal(String path, File file) {
		File file1 = new File(servletContext.getRealPath(path));
		try {
			FileUtils.copyFile(file, file1);
		} catch (IOException ioexception) {
			ioexception.printStackTrace();
			throw new ApplicationException("上传文件失败!");
		}
	}
	
	@Override
	public String getPath(String file_path){
		JSONObject configs = getConfig();
		if (configs != null && configs.get("path") != null) {
			file_path = configs.getString("path") + file_path;
		}else{
			file_path = servletContext.getRealPath(file_path);
		}
		return file_path;
	}


	public String getType() {
		return "storage";
	}


	public void perform(Object... params) {
		
	}


	public void register() {
		
	}


	public String getName() {
		return "filePlugin";
	}


	public String getVersion() {
		return "1.0";
	}


	public String getAuthor() {
		return "yangrongchang";
	}
}
